﻿/*  Copyright © 2012 Matthew David Elgert - mdelgert@yahoo.com
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */

using System;
using Microsoft.Win32.TaskScheduler;

namespace BLL
{
    /// <summary>
    /// Based on: http://taskscheduler.codeplex.com/
    /// </summary>

    public static class Scheduler
    {

        public static void AddWindowsSchedule(DateTime start)
        {
            
            try
            {
                using (TaskService ts = new TaskService())
                {
                    // Create a new task definition and assign properties
                    TaskDefinition td = ts.NewTask();
                    td.RegistrationInfo.Description = "SQLBackupFTP http://sqlbackupftp.codeplex.com Copyright © 2012 Matthew David Elgert - mdelgert@yahoo.com";
                    // Create a trigger that will fire the task at this time every other day
                    td.Triggers.Add(new DailyTrigger { DaysInterval = 1, StartBoundary = start });
                    // Create an action that will launch Notepad whenever the trigger fires
                    td.Actions.Add(new ExecAction("C:\\Program Files (x86)\\SQLBackupFTP\\SQLBackupFTP.exe", null, "C:\\Program Files (x86)\\SQLBackupFTP\\"));
                    
                    //Windows 7 is giving hard time with this come back to this later
                    //td.Principal.RunLevel = TaskRunLevel.Highest;
                    //td.Principal.LogonType = TaskLogonType.S4U;
                    
                    // Register the task in the root folder
                    ts.RootFolder.RegisterTaskDefinition(@"SQLBackupFTP", td);
                    Console.WriteLine("Scheduled SQLBackupFTP Task To Run Daily Starting " + start);
                }

            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString(), true);
            }

        }

        public static void RemoveWindowsSchedule()
        {
            try
            {
                using (TaskService ts = new TaskService())
                {
                    ts.RootFolder.DeleteTask("SQLBackupFTP");
                    Console.WriteLine("SQLBackupFTP Task Removed");
                }
            }

            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString(), true);
            }
        }
    
    }

}
